﻿using Quartz;
using Quartz.Listener;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace YnPlcDataServer.Scheduler
{
    public class MySchedulerListener : ISchedulerListener
    {
        #region 事件
        /// <summary>Job Add</summary>
        public event EventHandler<ITriggerEventArgs> OnJobScheduled;
        /// <summary>Job Add</summary>
        public event EventHandler<TriggerKeyEventArgs> OnJobUnscheduled;

        /// <summary>Job Add</summary>
        public event EventHandler<IJobDetailEventArgs> OnJobAdded;
        /// <summary>Job Delete</summary>
        public event EventHandler<JobKeyEventArgs> OnJobDeleted;
        /// <summary>Job Paused</summary>
        public event EventHandler<JobKeyEventArgs> OnJobPaused;
        /// <summary>Job Resumed</summary>
        public event EventHandler<JobKeyEventArgs> OnJobResumed;

        /// <summary>调度任务已经启动</summary>
        public event EventHandler<EventArgs> OnSchedulerStarted;
        /// <summary>调度任务已经停止</summary>
        public event EventHandler<EventArgs> OnSchedulerShutdown;
        #endregion
        
        #region Job
        public async Task JobScheduled(ITrigger trigger, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobScheduled(this, new ITriggerEventArgs(trigger));
            });
        }
        public async Task JobUnscheduled(TriggerKey triggerKey, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobUnscheduled(this, new TriggerKeyEventArgs(triggerKey));
            });
        }
        public async Task JobAdded(IJobDetail jobDetail, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobAdded(this, new IJobDetailEventArgs(jobDetail));
            });
        }
        public async Task JobDeleted(JobKey jobKey, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobDeleted(this, new JobKeyEventArgs(jobKey));
            });
        }
        public async Task JobInterrupted(JobKey jobKey, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        public async Task JobPaused(JobKey jobKey, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobPaused(this, new JobKeyEventArgs(jobKey));
            });
        }
        public async Task JobsPaused(string jobGroup, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        public async Task JobResumed(JobKey jobKey, CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                OnJobResumed(this, new JobKeyEventArgs(jobKey));
            });
        }
        public async Task JobsResumed(string jobGroup, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        #endregion

        #region Trigger
        public async Task TriggerFinalized(ITrigger trigger, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }

        public async Task TriggerPaused(TriggerKey triggerKey, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }

        public async Task TriggersPaused(string triggerGroup, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }

        public async Task TriggerResumed(TriggerKey triggerKey, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }

        public async Task TriggersResumed(string triggerGroup, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        #endregion

        #region 调度任务
        public async Task SchedulerError(string msg, SchedulerException cause, CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        public async Task SchedulerInStandbyMode(CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        public async Task SchedulerStarted(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                TaskManager.Instance.taskState = TaskState.Start;
                OnSchedulerStarted(this, null);
            });
        }
        public async Task SchedulerStarting(CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        public async Task SchedulerShutdown(CancellationToken cancellationToken)
        {
            await Task.Run(() =>
            {
                TaskManager.Instance.taskState = TaskState.Shutdown;
                OnSchedulerShutdown(this, null);
            });
        }
        public async Task SchedulerShuttingdown(CancellationToken cancellationToken)
        {
            await Task.Run(() =>{});
        }
        public async Task SchedulingDataCleared(CancellationToken cancellationToken)
        {
            await Task.Run(() => { });
        }
        #endregion
    }
}
